package org.mozhu.zufang.system.auth.service;

import com.google.common.collect.Sets;
import org.mozhu.zufang.system.privilege.entity.Permission;
import org.mozhu.zufang.system.privilege.entity.Role;
import org.mozhu.zufang.system.privilege.service.PermissionService;
import org.mozhu.zufang.system.privilege.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public abstract class AbstractAuthorityService implements AuthorityService {

    @Autowired
    RoleService roleService;

    @Autowired
    PermissionService permissionService;

    protected Set<String> stringRoles(List<Role> roles) {
        return roles.stream().map(Role::getCode).collect(Collectors.toSet());
    }

    protected Set<String> stringPermissions(List<Permission> permissions) {
        return permissions.stream().map(Permission::getCode).collect(Collectors.toSet());
    }

    protected Set<Long> rolesToPermissionIds(List<Role> roles) {
        Set<Long> permissionIds = Sets.newHashSet();
        for (Role role : roles) {
            permissionIds.addAll(role.getPermissionIds());
        }
        return permissionIds;
    }
}
